package fr.generali.sas.controller.v1_0;

import javax.annotation.Resource;
import javax.xml.bind.JAXBElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import fr.generali.sas.facade.v1_0.SASFluxSyncFacade;
import fr.generali.sas.model.v1_0.InfoClientResponse;
import fr.generali.sas.model.v1_0.NotificationIntegrationResponseType;
import fr.generali.sas.service.flux.FluxSynchroService;

@Controller
public class FluxController extends ExceptionHandledController implements InitializingBean{
	
	@Resource
	FluxSynchroService fluxSyncServiceSimple;
	
	private Logger logger = LoggerFactory.getLogger(FluxController.class);
	
	@Resource
	SASFluxSyncFacade sasFluxSyncFacade;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(fluxSyncServiceSimple, "fluxSyncServiceSimple should not be null");
		
	}
	
	
	//no content negotiation => json  ok BUT xml KO. (xml KO => Erreur ok car return ModelAndView)  
	@RequestMapping(method=RequestMethod.GET, value="/public/v1/sas/recupererFluxClient/{codeIntermediaire}/{idClient}",headers="Accept=application/json,application/xml")
	public @ResponseBody JAXBElement<InfoClientResponse> getFluxClient(@PathVariable String codeIntermediaire,@PathVariable String idClient) throws Exception{
		JAXBElement<InfoClientResponse> response = null;
		if(fluxSyncServiceSimple!=null) {
			response = this.fluxSyncServiceSimple.makeRecupererClientResponse(codeIntermediaire, idClient);
		}else {
			logger.error(" method:fluxSyncServiceSimple should not be null");
		}
		return response;
		
	}
	
	//same as above
	@RequestMapping(method=RequestMethod.GET, value="/public/v2/sas/recupererFluxClient/{codeIntermediaire}/{idClient}",headers="Accept=text/xml, application/xml, application/json")
	public @ResponseBody InfoClientResponse getFluxClient2(@PathVariable String codeIntermediaire,@PathVariable String idClient) throws Exception {
		JAXBElement<InfoClientResponse> response = null;
		if(fluxSyncServiceSimple!=null) {
			response = this.fluxSyncServiceSimple.makeRecupererClientResponse(codeIntermediaire, idClient);
		}else {
			logger.error(" method:fluxSyncServiceSimple should not be null");
		}
		return response.getValue();
		
	}
	
	//content negotiation => json ok But xml ko because infoClientResponse is not a xmlRootElement
	@RequestMapping(method=RequestMethod.GET, value="/public/v3/sas/recupererFluxClient/{codeIntermediaire}/{idClient}")
	public InfoClientResponse getFluxClient3(@PathVariable String codeIntermediaire,@PathVariable String idClient) throws Exception {
		JAXBElement<InfoClientResponse> response = null;
		if(fluxSyncServiceSimple!=null) {
			response = this.fluxSyncServiceSimple.makeRecupererClientResponse(codeIntermediaire, idClient);
		}else {
			logger.error(" method:fluxSyncServiceSimple should not be null");
		}
		return response.getValue();
		
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/public/v4/sas/recupererFluxClient/{codeIntermediaire}/{idClient}",headers="Accept=application/json,application/xml")
	public JAXBElement<InfoClientResponse> getFluxClient4(@PathVariable String codeIntermediaire,@PathVariable String idClient) throws Exception{
		JAXBElement<InfoClientResponse> response = null;
		//response = this.fluxSyncServiceSimple.makeRecupererClientResponse(codeIntermediaire, idClient);
		response = this.sasFluxSyncFacade.makeRecupererClientResponse(codeIntermediaire, idClient);
		return response;
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/public/v4/sas/recupererFluxAgence/{codeIntermediaire}",headers="Accept=application/json,application/xml")
	public JAXBElement<InfoClientResponse> getFluxAgence(@PathVariable String codeIntermediaire) throws Exception{
		JAXBElement<InfoClientResponse> response = null;
		response = this.fluxSyncServiceSimple.makeRecupererAgenceResponse(codeIntermediaire);
		return response;
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/public/v4/sas/poursuivreRecuperationFlux/{codeIntermediaire}/{numeroReservation}/{fromId}",headers="Accept=application/json,application/xml")
	public JAXBElement<InfoClientResponse> poursuivreRecuperationFlux(@PathVariable String codeIntermediaire,@PathVariable String numeroReservation,@PathVariable String fromId) throws Exception{
		JAXBElement<InfoClientResponse> response = null;
		response = this.fluxSyncServiceSimple.makePoursuivreResponse(codeIntermediaire, numeroReservation, fromId);		
		return response;
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/public/v4/sas/acquitterSucces/{codeIntermediaire}/{numeroReservation}",headers="Accept=application/json,application/xml")
	public JAXBElement<NotificationIntegrationResponseType> acquitterSuccesIntegrationFlux(@PathVariable String codeIntermediaire,@PathVariable String numeroReservation) throws Exception{
			JAXBElement<NotificationIntegrationResponseType> response = this.fluxSyncServiceSimple.makeNotificationIntegrationResponse(codeIntermediaire,
				numeroReservation, this.fluxSyncServiceSimple.acquitteSuccesIntegrationFlux(codeIntermediaire, numeroReservation),true);
		return response;
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/public/v4/sas/acquitterEchec/{codeIntermediaire}/{numeroReservation}",headers="Accept=application/json,application/xml")
	public JAXBElement<NotificationIntegrationResponseType> acquitterEchecIntegrationFlux(@PathVariable String codeIntermediaire,
			@PathVariable String numeroReservation) throws Exception {
		JAXBElement<NotificationIntegrationResponseType> response = this.fluxSyncServiceSimple.makeNotificationIntegrationResponse(codeIntermediaire,
				numeroReservation, this.fluxSyncServiceSimple.acquitteEchecIntegrationFlux(codeIntermediaire, numeroReservation), false);
		return response;

	}

	

}
